﻿package com.dbm.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.dbm.model.ConnPoolFactory;
import com.dbm.util.LsInfo;

/**
 * 流水请求数据处理Dao
 * @author wqying
 *
 */
public class LsDao {
	
	/**
	 * 新增订单流水请求lsid
	 * @param lsId
	 * @return
	 * @throws Exception
	 */
	public synchronized boolean addLsParam(int id,String lsId) throws Exception{
		boolean flag = false;
		Connection ctlconn = ConnPoolFactory.getCtlConnection();
		String lsSql = "INSERT INTO LSHTTPPOST VALUES (?,?,'','N','N')";
		PreparedStatement ctlstat = ctlconn.prepareStatement(lsSql);
		ctlstat.setInt(1, id);
		ctlstat.setString(2, lsId);
		if(ctlstat.executeUpdate()==1){
			flag = true;
		}
		ctlstat.close();
		ctlconn.close();
		return flag;
	}
	
	/**
	 * 获取所有订单流水请求
	 * @return
	 * @throws Exception
	 */
	public synchronized List<LsInfo> findLsIdNoSend() throws Exception{
		List<LsInfo> LsInfos = new ArrayList<LsInfo>();
		Connection ctlconn = ConnPoolFactory.getCtlConnection();
		String sql = "SELECT * FROM LSHTTPPOST WHERE sendflag = 'N'";
		PreparedStatement ctlstat = ctlconn.prepareStatement(sql);
		ResultSet rs = ctlstat.executeQuery();
		while(rs.next()){
			LsInfo lsInfo = new LsInfo();
			lsInfo.setId(rs.getInt("id"));
			lsInfo.setIfsuccess(rs.getString("ifsuccess"));
			lsInfo.setParam(rs.getString("param"));
			LsInfos.add(lsInfo);
		}
		rs.close();
		ctlstat.close();
		ctlconn.close();
		return LsInfos;
	}
	/**
	 * 更新订单流水状态为已发送，并记录请求
	 * @param lsInfo
	 * @return
	 * @throws Exception
	 */
	public synchronized boolean upLsSendFlag(LsInfo lsInfo) throws Exception{
		boolean flag = false;
		Connection ctlconn = ConnPoolFactory.getCtlConnection();
		String sql = "UPDATE LSHTTPPOST T SET T.RESULT=?,T.SENDFLAG='Y' WHERE ID=?";
		PreparedStatement ctlstat = ctlconn.prepareStatement(sql);
		ctlstat.setString(1, lsInfo.getResult());
		ctlstat.setInt(2, lsInfo.getId());
		if(ctlstat.executeUpdate()==1){
			flag = true;
		}
		ctlstat.close();
		ctlconn.close();
		return flag;
	}
}
